
#import "TorrentPeerDetailsViewController.h"
#import "UserDetailsViewController.h"

@implementation TorrentPeerDetailsViewController

//
// torrent info
//
@synthesize torrentInfo														= _torrentInfo;

//
// peer info
//
@synthesize peerInfo														= _peerInfo;

//
// config
//
@synthesize peerInfoRows													= _peerInfoRows;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
	
	//
	// setup title
	//
	self.title																= NSLocalizedStringFromTable(@"Peer Details", @"TorrentPeerDetailsViewController", nil);
	
	//
	// setup config
	//
	self.peerInfoRows														= [NSMutableArray arrayWithCapacity:10];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1>", @"resue", @"userName", @"value1", @"ipType", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Uploaded", @"title", @"uploadedSize", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Avg. Upload Rate", @"title", @"avgUploadRate", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Upload Rate", @"title", @"uploadRate", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Downloaded", @"title", @"downloadedSize", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Avg. Download Rate", @"title", @"avgDownloadRate", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Download Rate", @"title", @"downloadRate", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Share Ratio", @"title", @"shareRatio", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Completed", @"title", @"completed", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Connected", @"title", @"connectedTime", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Idle", @"title", @"idleTime", @"value2", nil]];
	[self.peerInfoRows addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"resue", @"Client", @"title", @"client", @"value2", nil]];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.torrentInfo														= nil;
	self.peerInfo															= nil;
	self.peerInfoRows														= nil;
	
	[super dealloc];
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

//
// get title
//
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
	return self.torrentInfo.title;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return self.peerInfoRows.count;
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	NSDictionary* config													= [self.peerInfoRows objectAtIndex:indexPath.row];
	NSString* reuseId														= [config valueForKey:@"resue"];
	UITableViewCell* theCell												= [tableView dequeueReusableCellWithIdentifier:reuseId];
	if(!theCell)
		theCell																= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseId] autorelease];
	
	NSString* temp															= [config valueForKey:@"title"];
	BOOL selectable															= !temp.length && self.peerInfo.userId;
	if(temp.length)
		theCell.textLabel.text												= NSLocalizedStringFromTable(temp, @"TorrentPeerDetailsViewController", nil);
	else
		theCell.textLabel.text												= [self.peerInfo valueForKey:[config valueForKey:@"value1"]];
	
	theCell.detailTextLabel.text											= [self.peerInfo valueForKey:[config valueForKey:@"value2"]];
	theCell.accessoryType													= selectable ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
	theCell.selectionStyle													= selectable ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;
	
	return theCell;
}

//
// select row
//
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
	NSDictionary* config													= [self.peerInfoRows objectAtIndex:indexPath.row];
	if(![[config valueForKey:@"title"] length] && self.peerInfo.userId)
	{
		UserDetailsViewController* userDetailsViewController				= [[[UserDetailsViewController alloc] init] autorelease];
		userDetailsViewController.userId									= self.peerInfo.userId;
		[self.navigationController pushViewController:userDetailsViewController animated:YES];
	}
}

@end
